package com.resou.reader.bookshelf.dialogutil;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.api.entry.ShareTimeBean;
import com.resou.reader.assist.WebActivity;
import com.resou.reader.bookshelf.bookedit.BookShelfEditActivity;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.eventbus.BookShelfEvent;
import com.umeng.socialize.ShareAction;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MenuClickDialog extends DialogFragment {
    Unbinder unbinder;
    Context mContext;
    @BindView(R.id.ll_batch_manage)
    LinearLayout llBatchManage;
    @BindView(R.id.iv_list_icon)
    ImageView ivListIcon;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.ll_list_show)
    LinearLayout llListShow;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_bookshelf_menu, container, false);
        unbinder = ButterKnife.bind(this, view);

        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.END | Gravity.TOP;
            params.y = 120;
            getDialog().getWindow().setAttributes(params);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            getDialog().setCanceledOnTouchOutside(true);
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        initView();

        return view;
    }

    private void initView() {
        if (PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).getIsListShow()) {
            tvListTitle.setText("图墙模式");
            ivListIcon.setImageResource(R.mipmap.icon_more2);
        } else {
            tvListTitle.setText("列表模式");
            ivListIcon.setImageResource(R.mipmap.icon_more3);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.ll_batch_manage, R.id.ll_list_show, R.id.ll_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_batch_manage:
                BookShelfEditActivity.startActivity(mContext);
                dismiss();
                break;
            case R.id.ll_list_show:
                if (PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).getIsListShow()) {
                    PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).setIsListShow(false);
                } else {
                    PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).setIsListShow(true);
                }
                //通知其他界面更新
                BookShelfEvent event = new BookShelfEvent();
                event.isListShow = true;
                EventBus.getDefault().post(event);
                dismiss();
                break;
            case R.id.ll_share:
                if (NetworkUtils.isNetworkConnected(getActivity())) {
                    EventBus.getDefault().post(new ShareTimeBean());
                    dismiss();
                } else {
                    ToastUtil.makeShortToast("网络异常");
                }
                break;
        }
    }
}
